package com.ruoyi.web.controller.system;


import cn.hxzy.common.util.RandomId;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hxzy.system.entity.TdCooperationModel;
import com.hxzy.system.service.TdCooperationModelService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.DataSourceType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mingqian.Wu
 * @since 2023-06-24
 */
@RestController
@RequestMapping("/tdCooperationModel")
@DataSource(DataSourceType.SYSTEM)
public class TdCooperationModelController {

    @Autowired
    TdCooperationModelService cooperationModelService;


    @GetMapping("getCooperationList")
    public AjaxResult getCooperationList(){
        List<TdCooperationModel> list = cooperationModelService.list();
        return AjaxResult.success(list);
    }


    @GetMapping("getCooperationInfo/{id}")
    public AjaxResult getCooperationInfo(@PathVariable String id){
        TdCooperationModel byId = cooperationModelService.getById(id);
        return AjaxResult.success(byId);
    }

    @PutMapping("updateCooperation")
    public AjaxResult updateCooperation(@RequestBody TdCooperationModel tdCooperationModel){
        UpdateWrapper<TdCooperationModel>updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",tdCooperationModel.getId());
        boolean update = cooperationModelService.update(tdCooperationModel, updateWrapper);
        if (update){
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("修改失败");
    }

    @PostMapping("addCooperation")
    public AjaxResult addCooperation(@RequestBody TdCooperationModel tdCooperationModel){
       tdCooperationModel.setId(RandomId.getCooperateId());
        boolean update = cooperationModelService.save(tdCooperationModel);
        if (update){
            return AjaxResult.success("添加成功");
        }
        return AjaxResult.error("添加失败");
    }



    @GetMapping("getCooperationByName/{name}")
    public AjaxResult getCooperationByName(@PathVariable  String name){
        QueryWrapper<TdCooperationModel>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",name);
        List<TdCooperationModel> list = cooperationModelService.list(queryWrapper);
        return AjaxResult.success(list);
    }

    @DeleteMapping("deleteCooperation/{id}")
    public AjaxResult deleteCooperation(@PathVariable  String id){
        boolean b = cooperationModelService.removeById(id);
        if (b){
            return AjaxResult.success("删除成功");
        }
        return AjaxResult.error("删除失败");
    }





}

